<template>
  <div class="role-management">
    <div class="page-header">
      <h2>角色管理</h2>
      <p>管理系统角色信息和权限分配</p>
    </div>
    
    <a-card>
      <div class="actions" style="margin-bottom: 16px; text-align: right;">
        <a-button type="primary" @click="handleAdd">
          <PlusOutlined />
          新增角色
        </a-button>
      </div>
      
      <a-table
        :columns="columns"
        :data-source="roleList"
        :loading="loading"
        row-key="id"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'permissions'">
            <a-tag v-for="permission in record.permissions.slice(0, 3)" :key="permission.id">
              {{ permission.name }}
            </a-tag>
            <a-tag v-if="record.permissions.length > 3">
              +{{ record.permissions.length - 3 }}
            </a-tag>
          </template>
          
          <template v-if="column.key === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="handleEdit(record)">
                编辑
              </a-button>
              <a-popconfirm
                title="确定要删除这个角色吗？"
                @confirm="handleDelete(record)"
              >
                <a-button type="link" size="small" danger>
                  删除
                </a-button>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { PlusOutlined } from '@ant-design/icons-vue'

const loading = ref(false)

const roleList = ref([
  {
    id: 1,
    name: '管理员',
    code: 'admin',
    description: '系统管理员，拥有所有权限',
    permissions: [
      { id: 1, name: '用户管理' },
      { id: 2, name: '角色管理' },
      { id: 3, name: '权限管理' }
    ]
  },
  {
    id: 2,
    name: '编辑者',
    code: 'editor',
    description: '内容编辑者，可以编辑内容',
    permissions: [
      { id: 1, name: '内容查看' },
      { id: 2, name: '内容编辑' }
    ]
  }
])

const columns = [
  { title: 'ID', dataIndex: 'id', key: 'id', width: 80 },
  { title: '角色名称', dataIndex: 'name', key: 'name' },
  { title: '角色编码', dataIndex: 'code', key: 'code' },
  { title: '描述', dataIndex: 'description', key: 'description' },
  { title: '权限', key: 'permissions' },
  { title: '操作', key: 'action', width: 150 }
]

const handleAdd = () => {
  console.log('新增角色')
}

const handleEdit = (record: any) => {
  console.log('编辑角色', record)
}

const handleDelete = (record: any) => {
  console.log('删除角色', record)
}
</script>

<style scoped>
.role-management {
  padding: 0;
}

.page-header {
  margin-bottom: 24px;
}

.page-header h2 {
  margin: 0 0 8px 0;
  color: #262626;
}

.page-header p {
  margin: 0;
  color: #8c8c8c;
}
</style> 